home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Commodore Free 10
/
Commodore_Free_Issue_10_2007_Commodore_Computer_Club.d64
/
t.slang tut
< prev
next >
Wrap
Text File
|
2023-02-26
|
11KB
|
395 lines
u SLANG TUTORIAL PART 1
A simple tutorial:
Transitioning to Slang from BASIC
======================================
If you're familiar with higher-level
languages you can probably skip this
part. Some people however may be
familiar primarily with programming in
Commodore BASIC, and that's who this
section is targeted at. This section
introduces some of the higher-level
language features in Slang you'll need
to be aware of, in comparison with
BASIC.
This is also a little tutorial on
using Slang, so there are lots of
examples below to try out. Once you've
gone through them you should be well
on your way to using Slang like a pro.
Note: If you find this tutorial useful
-- or if you find it totally confusing
or if you find it anything at all --
please write me or post a note to the
forum, and let me know. I'd love to
hear from you. Heck, right now I'd
love to hear from anybody! But
feedback is very important, and it
helps me to know what works and what
doesn't.
Interpreted vs. Compiled
BASIC is an interpreted language,
whereas Slang is a compiled language.
In BASIC, you write the code, then RUN
it, then errors are caught as they are
encountered (Syntax Errors, Overflow
Errors, etc.). You can break a program
while it's running, have a look at
some of the variables, change things
around, then CONTinue running the
program.
In a compiled language like Slang you
write the code, then compile it into
machine language, then run that
machine language program. You don't
actually run the code you've written
-- the compiler converts the code
you've written into machine language,
which you then run.
So there are actually two parts to the
program, that you can for example save
to disk: the Dsource codeD,which is
the part you write, and the Dobject
codeD which is what the compiler
produces. The object code is what
other people will run whenthey run
your program. In Slang, source code
files end in '.s', as in
"spritedemo.e.s", and object files end
in '.o', as in "spritedemo.e.o"
As to errors, errors such as Syntax
Errors are caught during the
DcompileDphase, before ever running
the program. Other types of errors
(called runtime errors) are never
caught at all -- in general if there
are errors in your program it will
keep on running, or may lock up the
machine entirely! But Slang is
designed to handle this, so that you
won't lose your program.
Tutorial lesson #1:
So here is the first thing to try:
we're going to write a program that
will crash, so you can lose your fear
right away of causing some huge
problem. To start up slang, load and
run the main program (slangb1.9.o or
whatever it might be). Once you've got
the editor running, type in the
following program:
sprint "here comes a crash! "
donebrk
Then press F1 to compile the program.
If there are no errors, you will get a
"compile successful" message. Now
press F4 to run the program - crash,
right? Go ahead and reset the machine
(don't power it down; just press the
reset button). Then type
sys 54016
and you should pop back into Slang.
Slang stores itself up in SuperRAM, so
you don't need to worry about losing
your program because of a crash, and
if you want to try something just go
ahead and experiment!
Variables
In BASIC variables
are created on the fly -- you just say
"10 a=1" and off you go. In languages
like Slang, you have to declare your
variables before using them. Let's
take a dumb BASIC program, and convert
it to Slang:
10 FOR B=1 TO 3:PRINT "BLAH ";:NEXT
20 PRINT CHR$(13)+"PRESS ANY KEY" 30
GET A$:IF A$="" THEN 30 40 END
Obviously lines 20-40 are not needed,
but I put them in for a reason.
Here's a Slang version:
byte b for b=1:3 sprint "blah " next
sprint !13"press any key..." waitchar
done
Tutorial lesson #2:
Go ahead and type this program into
the Slang editor, press F1 to compile,
and press F4 to run. Easy! The first
line of this program DdeclaresD the
variable. Part of the reason you have
to do this is that there are different
Dvariable typesD available. In BASIC,
you can have floating-point variables,
integers, and strings, and the name
itself tells basic what type of
variable it is:
a=10 ;numeric (floating point)
variable a%=10 ;numeric (integer)
variable a$="10" ;string variable
As you probably know, the only reason
to use integer variables is with
arrays, to save memory, because an
integer array takes less memory than a
float. Otherwise, a plain integer
variable takes exactly as much space
as a regular variable, and is actually
slower for calculations because all
calculations in BASIC are
floating-point calculations.
In Slang, this is not the case. The
variable type not only changes how
much space is used but also how the
variable is manipulated. In Slang,
adding integers is much faster than
adding floats together, and the
integers take less space. For example,
if you change the line
byte b
in the program above to
float b
you'll find that the program becomes
larger, and it's also much slower
(although you won't notice that in a
simple program like this)
Tutorial lesson #3:
Change b from a byte to a float,
compile, and see what happens. The
different variable types available in
Slang are:
byte - 1 byte, signed numbers in range
-128..127 ubyte - 1 byte, unsigned,
range = 0..255 int - 2 bytes, signed,
range = -32768..32767 uint - 2 bytes,
unsigned, range = 0..65535 float - 5
bytes, signed, range = well, the usual
BASIC range
In general, the smaller variables are
also faster, so you'll often choose
the simplest variable that meets your
needs. Just a few other things are
worth noting. Unlike BASIC, variables
can also have really long names, like:
uint ThisIsAVeryLongVariableName
b = b+ThisIsAVeryLongVariableName
Note also that variables can mix upper
and lower case (the compiler is case-
insensitive), and can mix variables of
different types (you can add a byte to
an int, for example). And once a
variable is declared, that's it -- you
can't re define a variable as a
different type.
One other thing to notice is that
there is only one statement per line.
Unlike BASIC, you cannot put multiple
commands on the same line.
Loops and such
As is apparent in the above program,
the for-loop in Slang is pretty
similar to the BASIC for-loop. You can
also put a "step" in there:
for b=1:10 step 3
There are two other loop structures
available in Slang (and many other
lanauges): while-loops, and
repeat-loops. Here's an example:
b=1 while b<4 sprint "blah " b=b+1
endwhile
This program does exactly the same
thing that the for-loop does in the
original program: while the expression
b<4 is true, it performs whatever code
is in-between the "while" and
"endwhile" statements. The repeat
statement is very similar:
b=1 repeat sprint "blah" b=b+1 until
b>3
A repeat-statement always executes at
least once, because the expression
check is at the end of the loop. The
way to think about all these things
is pretty simple:
while [expression] While [expression]
is true [code] <-------------Execute
this block of code endwhile between
the while and endwhile statements One
thing that Slang does not have is a
GOTO statement. You can actually do
gotos using assembly language, but it
turns out that you can handle all the
things you might use GOTO for using
for/repeat/while-loops and the
if-endif structure discussed down
below, and your programs will be
easier to debug.
Tutorial lesson #4:
Replace the for-loop in the example
code with a) a while-loop, and b) a
repeat- until loop, but make it print
out the text five times instead of
three. Compile and run each case to
verify that it works.
Ending a program You'll notice that
the program ends with the line "done".
In BASIC, putting an "END" in is
optional. In Slang, it is required. It
tells the C64 how and when to exit the
program. If you'd like to see what
happens when you leave it out, go
ahead and try it! Just remember that
"sys 54016" will restart Slang after
you reset the computer.
We can now explain the last few lines
as well. The command "waitchar" simply
waits for a key to be pressed. Without
this line, the program would
immediately end and return you back to
Slang. The "waitchar" would not be
necessary if this program were being
called from, say, BASIC -- in
thatcase, you would probably want
control to return to BASIC
immediately.
But when running programs directly
from the Slang editor, you'll usually
wantto put a line like this at the
end of the program.
Tutorial lesson #5:
As a test, go ahead and comment out
that line (place a ";" before the
waitchar re-compile, and see what
happens when you run the program!
Print The little program above used
the "sprint" command. There are
actually two print commands in Slang:
print, and sprint.
Sprint -- Simple Print, or String
Print -- is a simple print command
that only prints strings (not
numbers). As we shall see shortly, the
regular print command contains certain
things in a DlibraryD, which requires
an extra step, and will make your
programs larger. For the moment though
we can focus on sprint.
Printing a string in Slang works much
the same in BASIC, with just a few
little twists. Unlike in BASIC, you
cannot embed special characters inside
the quotes, like
10 PRINT "[ctrl-2][shft-clr]nice
white text on a clear screen."
Instead, in Slang, you have to use the
character codes directly; in BASIC,
you could also do the above as
10 PRINT CHR$(5)+CHR$(147)+"nice
white text on a clear screen."
In Slang, you simply use a ! instead
of chr$, so this would look like
sprint !5!147"nice white text on a
clear screen."
The above command has one key
difference from the BASIC version,
however :in BASIC, PRINT normally
prints a chr$(13) at the end of the
line, whereassprint will not. It turns
out that there's actually two more
print commands in Slang, though:
println and sprintln. These work
exactly the same as print and sprint,
but print an extra [RETURN] character
at the end.
Tutorial lesson #6:
Go ahead and try replacing "sprint"
with "sprintln" in the example
program,and see what happens.
Tutorial lesson #7:
Modify the example program to print
out the text in cyan. How about light
green?
There's one more trick to print and
sprint: you can specify DwhereD on
the screen to print:
sprint(0,20) "some text"
will print the text at row=0,
column=20.
Tutorial lesson #8:
Modify the program to print the text
to the middle of the screen. In
contrast to sprint, print can print
numbers and strings, and you can stick
them all together on a single line.
The catch is that manyof the routines
needed to do this are in a DlibraryD.
This library is a file on the disk,
containing special routines. In
general, libraries can be source code,
object code, or a special kind of file
(a relocatable file) for use by the
linker, which I won't talk about here.
CONTINUED IN PART 2